package com.fita.admin.entity.search;

import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.entity.wallet.TransactionType;

import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/29
 */
public class TransactionRecordSearch extends AbstractSearch {

    private List<TransactionType> types;

    private List<Long> agentCompanyIds;

    private String mobile;

    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    @Override
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (refresh || this.filter == null){

            this.filter = new QueryFilter();

            this.filter.addSentence("tr.transactionType IN (:types)", "types", types);

            this.filter.addSentence("tr.targetUserWallet.agentCompany.id IN (:agentCompanyIds)", "agentCompanyIds", agentCompanyIds);

            this.filter.addSentence("tr.targetUserWallet.agentCompany.id IN (SELECT company.id FROM Agent WHERE mobile = :mobile )", "mobile", mobile);
        }
        return this.filter;
    }

    /**
     * get order
     *
     * @return
     */
    @Override
    public QueryOrder getOrder() {
        QueryOrder order = super.getOrder();
        order.add("tr.createTime", "DESC");
        return order;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    @Override
    public String buildSentenceTemplate() {
        return " FROM TransactionRecord tr LEFT JOIN DistributionRewardRecord drd ON tr.distributionRewardRecord.id = drd.id WHERE ((tr.transactionType <> 'AGENT_DISTRIBUTION_REWARD') OR (drd.status = 'CONSUMED')) AND tr.disabled = false  " + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
    }

    public List<TransactionType> getTypes() {
        return types;
    }

    public void setTypes(List<TransactionType> types) {
        this.types = types;
    }

    public List<Long> getAgentCompanyIds() {
        return agentCompanyIds;
    }

    public void setAgentCompanyIds(List<Long> agentCompanyIds) {
        this.agentCompanyIds = agentCompanyIds;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
